<?php
/**
 * Created by PhpStorm.
 * User: 庆金
 * Date: 2019-12-12
 * Time: 23:39
 */
$http = new swoole_http_server("0.0.0.0",8811);
$http->set([
    'enable_static_handler' => true,
    'document_root' =>  '/var/www/html/swoole/thinkphp/public',
    'worker_num'    =>  5,
]);
$http->on("WorkerStart",function (swoole_server $server,$worker_id){
    define('APP_PATH', __DIR__ . '/../application/');;
    // 加载框架引导文件
    // ThinkPHP 引导文件
    // 加载基础文件
    require __DIR__ . '/../thinkphp/base.php';
});
$http->on('request',function ($request,$response) use($http){
    print_r($request);
    print_r($_SERVER);
    $_SERVER = [];
    if (isset($request->server)){
        foreach($request->server as $k => $v){
            $_SERVER[strtoupper($k)] = $v;
        }
    }
    if (isset($request->header)){
        foreach($request->header as $k => $v){
            $_SERVER[$k] = $v;
        }
    }
    $_GET = [];
    if (isset($request->get)){
        foreach($request->get as $k => $v){
            $_GET[$k] = $v;
        }
    }
    $_POST = [];
    if (isset($request->post)){
        foreach($request->post as $k => $v){
            $_POST[$k] = $v;
        }
    }
    ob_start();
    try{
        // 执行应用并响应
        think\Container::get('app', [APP_PATH])
            ->run()
            ->send();

    }catch (\Exception $e){

    }
//    echo "-module:".request()->module()."-controller:".request()->controller()."-action:".request()->action().PHP_EOL;
    $res = ob_get_contents();
    ob_end_clean();
//    print_r($request->get);
    $response->cookie("singwa","xssss",time() + 1800);
    $response->end($res);

//    $http->close();
});
$http->start();